package com.loc;

import android.app.Application;
import android.app.Notification;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.WebView;
import androidx.core.view.InputDeviceCompat;
import androidx.core.view.PointerIconCompat;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.AMapLocationQualityReport;
import com.amap.api.location.APSService;
import com.amap.api.location.UmidtokenInfo;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.gisocn.onemap.tdt.TDTConstant;
import com.github.mikephil.charting.utils.Utils;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicBoolean;

/* compiled from: AmapLocationManager.java */
/* loaded from: classes2.dex */
public final class d {
    private static boolean H = true;
    private static boolean J = false;
    private static AtomicBoolean K = new AtomicBoolean(false);
    public static volatile boolean g = false;
    private Context D;
    private g E;
    ew a;
    public c c;
    j k;
    Intent n;
    AMapLocationClientOption b = new AMapLocationClientOption();
    h d = null;
    private boolean F = false;
    private volatile boolean G = false;
    ArrayList<AMapLocationListener> e = new ArrayList<>();
    boolean f = false;
    public boolean h = true;
    public boolean i = true;
    public boolean j = true;
    Messenger l = null;
    Messenger m = null;
    int o = 0;
    private boolean I = true;
    b p = null;
    boolean q = false;
    AMapLocationClientOption.AMapLocationMode r = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy;
    Object s = new Object();
    fz t = null;
    boolean u = false;
    e v = null;
    private AMapLocationClientOption L = new AMapLocationClientOption();
    private i M = null;
    String w = null;
    private ServiceConnection N = new ServiceConnection() { // from class: com.loc.d.2
        @Override // android.content.ServiceConnection
        public final void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            try {
                d.this.l = new Messenger(iBinder);
                d.this.F = true;
                d.this.u = true;
            } catch (Throwable th) {
                ft.a(th, "ALManager", "onServiceConnected");
            }
        }

        @Override // android.content.ServiceConnection
        public final void onServiceDisconnected(ComponentName componentName) {
            d.this.l = null;
            d.this.F = false;
        }
    };
    AMapLocationQualityReport x = null;
    boolean y = false;
    boolean z = false;
    private volatile boolean O = false;
    a A = null;
    String B = null;
    boolean C = false;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* compiled from: AmapLocationManager.java */
    /* renamed from: com.loc.d$3  reason: invalid class name */
    /* loaded from: classes2.dex */
    public static /* synthetic */ class AnonymousClass3 {
        static final /* synthetic */ int[] a;

        static {
            int[] iArr = new int[AMapLocationClientOption.AMapLocationMode.values().length];
            a = iArr;
            try {
                iArr[AMapLocationClientOption.AMapLocationMode.Battery_Saving.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                a[AMapLocationClientOption.AMapLocationMode.Device_Sensors.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                a[AMapLocationClientOption.AMapLocationMode.Hight_Accuracy.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
        }
    }

    /* compiled from: AmapLocationManager.java */
    /* loaded from: classes2.dex */
    public class a extends Handler {
        public a(Looper looper) {
            super(looper);
        }

        /* JADX WARN: Multi-variable type inference failed */
        /* JADX WARN: Type inference failed for: r0v0 */
        /* JADX WARN: Type inference failed for: r0v1 */
        /* JADX WARN: Type inference failed for: r0v2, types: [java.lang.String] */
        /* JADX WARN: Type inference failed for: r0v3, types: [java.lang.String] */
        /* JADX WARN: Type inference failed for: r0v7 */
        @Override // android.os.Handler
        public final void handleMessage(Message message) {
            ?? r0 = 0;
            try {
                super.handleMessage(message);
                if (fs.h) {
                    Log.e("AMapLocationClient", "SERVICE_NOT_AVAILABLE");
                    return;
                }
                int i = message.what;
                if (i == 11) {
                    d.this.a(message.getData());
                } else if (i == 12) {
                    d.this.b(message);
                } else if (i == 1011) {
                    d.this.a(14, (Bundle) null);
                    d.this.g();
                } else {
                    try {
                        switch (i) {
                            case 1002:
                                d.this.c((AMapLocationListener) message.obj);
                                return;
                            case 1003:
                                d.this.j();
                                d.this.a(13, (Bundle) null);
                                return;
                            case 1004:
                                d.this.l();
                                d.this.a(14, (Bundle) null);
                                return;
                            case 1005:
                                d.this.d((AMapLocationListener) message.obj);
                                return;
                            default:
                                switch (i) {
                                    case PointerIconCompat.TYPE_HORIZONTAL_DOUBLE_ARROW /* 1014 */:
                                        d.this.a(message);
                                        return;
                                    case PointerIconCompat.TYPE_VERTICAL_DOUBLE_ARROW /* 1015 */:
                                        d.this.d.a(d.this.b);
                                        d.this.a((int) InputDeviceCompat.SOURCE_GAMEPAD, (Object) null, 300000L);
                                        return;
                                    case PointerIconCompat.TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW /* 1016 */:
                                        if (gb.m(d.this.D)) {
                                            fv.a();
                                            d.this.r();
                                            return;
                                        } else if (d.this.d.b()) {
                                            d.this.a((int) PointerIconCompat.TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW, (Object) null, 1000L);
                                            return;
                                        } else {
                                            d.this.n();
                                            return;
                                        }
                                    case PointerIconCompat.TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW /* 1017 */:
                                        d.this.d.a();
                                        d.this.a(InputDeviceCompat.SOURCE_GAMEPAD);
                                        return;
                                    case PointerIconCompat.TYPE_ZOOM_IN /* 1018 */:
                                        d.this.b = (AMapLocationClientOption) message.obj;
                                        if (d.this.b != null) {
                                            d.this.s();
                                            return;
                                        }
                                        return;
                                    default:
                                        switch (i) {
                                            case 1023:
                                                d.this.c(message);
                                                return;
                                            case 1024:
                                                d.this.d(message);
                                                return;
                                            case InputDeviceCompat.SOURCE_GAMEPAD /* 1025 */:
                                                if (d.this.d.f()) {
                                                    d.this.d.a();
                                                    d.this.d.a(d.this.b);
                                                }
                                                d.this.a((int) InputDeviceCompat.SOURCE_GAMEPAD, (Object) null, 300000L);
                                                return;
                                            case 1026:
                                                fv.b();
                                                d.this.E.a(d.this.b);
                                                return;
                                            case 1027:
                                                d.this.E.a();
                                                return;
                                            case 1028:
                                                d.this.g((AMapLocation) message.obj);
                                                return;
                                            case 1029:
                                                d.this.a(16, (Bundle) null);
                                                return;
                                            case 1030:
                                                d.this.a(17, (Bundle) null);
                                                return;
                                            default:
                                                return;
                                        }
                                }
                        }
                    } catch (Throwable th) {
                        r0 = message;
                        th = th;
                        if (r0 == 0) {
                            r0 = "handleMessage";
                        }
                        ft.a(th, "AMapLocationManage$MHandlerr", r0);
                    }
                }
            } catch (Throwable th2) {
                th = th2;
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* compiled from: AmapLocationManager.java */
    /* loaded from: classes2.dex */
    public static class b extends HandlerThread {
        d a;

        public b(String str, d dVar) {
            super(str);
            this.a = null;
            this.a = dVar;
        }

        @Override // android.os.HandlerThread
        protected final void onLooperPrepared() {
            try {
                this.a.k.a();
                fy.a(this.a.D);
                this.a.p();
                d dVar = this.a;
                if (dVar != null && dVar.D != null) {
                    fs.b(this.a.D);
                    fs.a(this.a.D);
                }
                super.onLooperPrepared();
            } catch (Throwable unused) {
            }
        }

        @Override // android.os.HandlerThread, java.lang.Thread, java.lang.Runnable
        public final void run() {
            try {
                super.run();
            } catch (Throwable unused) {
            }
        }
    }

    /* compiled from: AmapLocationManager.java */
    /* loaded from: classes2.dex */
    public class c extends Handler {
        public c() {
        }

        public c(Looper looper) {
            super(looper);
        }

        /* JADX WARN: Can't fix incorrect switch cases order, some code will duplicate */
        @Override // android.os.Handler
        public final void handleMessage(Message message) {
            try {
                super.handleMessage(message);
                if (d.this.q) {
                    return;
                }
                int i = message.what;
                if (i == 1) {
                    Message obtainMessage = d.this.A.obtainMessage();
                    obtainMessage.what = 11;
                    obtainMessage.setData(message.getData());
                    d.this.A.sendMessage(obtainMessage);
                    return;
                }
                if (i != 2) {
                    if (i == 13) {
                        if (d.this.a != null) {
                            d dVar = d.this;
                            dVar.a(dVar.a);
                            return;
                        }
                        AMapLocation aMapLocation = new AMapLocation("LBS");
                        aMapLocation.setErrorCode(33);
                        d.this.a(aMapLocation);
                        return;
                    }
                    switch (i) {
                        case 5:
                            Bundle data = message.getData();
                            data.putBundle("optBundle", ft.a(d.this.b));
                            d.this.a(10, data);
                            return;
                        case 6:
                            Bundle data2 = message.getData();
                            if (d.this.d != null) {
                                d.this.d.a(data2);
                                return;
                            }
                            return;
                        case 7:
                            d.this.I = message.getData().getBoolean("ngpsAble");
                            return;
                        case 8:
                            fz.a((String) null, 2141);
                            break;
                        case 9:
                            boolean unused = d.J = message.getData().getBoolean("installMockApp");
                            return;
                        case 10:
                            d.this.a((AMapLocation) message.obj);
                            return;
                        default:
                            switch (i) {
                                case 100:
                                    fz.a((String) null, 2155);
                                    break;
                                case 101:
                                    break;
                                case 102:
                                    Bundle data3 = message.getData();
                                    data3.putBundle("optBundle", ft.a(d.this.b));
                                    d.this.a(15, data3);
                                    return;
                                case 103:
                                    Bundle data4 = message.getData();
                                    if (d.this.E != null) {
                                        d.this.E.a(data4);
                                        return;
                                    }
                                    return;
                                default:
                                    return;
                            }
                            Message obtain = Message.obtain();
                            obtain.what = 1028;
                            obtain.obj = message.obj;
                            d.this.A.sendMessage(obtain);
                            if (d.this.L == null || !d.this.L.getCacheCallBack() || d.this.c == null) {
                                return;
                            }
                            d.this.c.removeMessages(13);
                            return;
                    }
                }
                Message obtain2 = Message.obtain();
                obtain2.what = 12;
                obtain2.obj = message.obj;
                d.this.A.sendMessage(obtain2);
                if (d.this.L == null || !d.this.L.getCacheCallBack() || d.this.c == null) {
                    return;
                }
                d.this.c.removeMessages(13);
            } catch (Throwable th) {
                ft.a(th, "AmapLocationManager$MainHandler", 0 == 0 ? "handleMessage" : null);
            }
        }
    }

    public d(Context context, Intent intent, Looper looper) {
        this.n = null;
        this.D = context;
        this.n = intent;
        b(looper);
    }

    private a a(Looper looper) {
        a aVar;
        synchronized (this.s) {
            aVar = new a(looper);
            this.A = aVar;
        }
        return aVar;
    }

    private ew a(er erVar, boolean z) {
        if (this.b.isLocationCacheEnable()) {
            try {
                return erVar.b(z);
            } catch (Throwable th) {
                ft.a(th, "ALManager", "doFirstCacheLoc");
                return null;
            }
        }
        return null;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void a(int i) {
        synchronized (this.s) {
            a aVar = this.A;
            if (aVar != null) {
                aVar.removeMessages(i);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void a(int i, Bundle bundle) {
        if (bundle == null) {
            try {
                bundle = new Bundle();
            } catch (Throwable th) {
                boolean z = (th instanceof IllegalStateException) && th.getMessage().contains("sending message to a Handler on a dead thread");
                if ((th instanceof RemoteException) || z) {
                    this.l = null;
                    this.F = false;
                }
                ft.a(th, "ALManager", "sendLocMessage");
                return;
            }
        }
        if (TextUtils.isEmpty(this.w)) {
            this.w = ft.b(this.D);
        }
        bundle.putString(TDTConstant.TILE_MATRIX_SET_2000, this.w);
        Message obtain = Message.obtain();
        obtain.what = i;
        obtain.setData(bundle);
        obtain.replyTo = this.m;
        Messenger messenger = this.l;
        if (messenger != null) {
            messenger.send(obtain);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void a(int i, Object obj, long j) {
        synchronized (this.s) {
            if (this.A != null) {
                Message obtain = Message.obtain();
                obtain.what = i;
                if (obj instanceof Bundle) {
                    obtain.setData((Bundle) obj);
                } else {
                    obtain.obj = obj;
                }
                this.A.sendMessageDelayed(obtain, j);
            }
        }
    }

    private static void a(final Context context) {
        if (K.compareAndSet(false, true)) {
            cr.a().b(new cs() { // from class: com.loc.d.1
                @Override // com.loc.cs
                public final void a() {
                    o.l();
                    o.a(context);
                    o.f(context);
                }
            });
        }
    }

    private void a(Intent intent) {
        try {
            this.D.bindService(intent, this.N, 1);
        } catch (Throwable th) {
            ft.a(th, "ALManager", "startServiceImpl");
        }
    }

    private void a(Intent intent, boolean z) {
        if (this.D != null) {
            if (Build.VERSION.SDK_INT >= 26 && z) {
                if (!t()) {
                    Log.e("amapapi", "-------------调用后台定位服务，缺少权限：android.permission.FOREGROUND_SERVICE--------------");
                    return;
                }
                try {
                    this.D.getClass().getMethod("startForegroundService", Intent.class).invoke(this.D, intent);
                } catch (Throwable unused) {
                }
                this.C = true;
            }
            this.D.startService(intent);
            this.C = true;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void a(Bundle bundle) {
        eq eqVar;
        AMapLocation aMapLocation;
        h hVar;
        AMapLocation aMapLocation2 = null;
        if (bundle != null) {
            try {
                bundle.setClassLoader(AMapLocation.class.getClassLoader());
                aMapLocation = (AMapLocation) bundle.getParcelable("loc");
                this.B = bundle.getString("nb");
                eqVar = (eq) bundle.getParcelable("statics");
                if (aMapLocation != null) {
                    try {
                        if (aMapLocation.getErrorCode() == 0 && (hVar = this.d) != null) {
                            hVar.c();
                            if (!TextUtils.isEmpty(aMapLocation.getAdCode())) {
                                h.y = aMapLocation;
                            }
                        }
                    } catch (Throwable th) {
                        th = th;
                        ft.a(th, "AmapLocationManager", "resultLbsLocationSuccess");
                        a(aMapLocation2, eqVar);
                    }
                }
            } catch (Throwable th2) {
                th = th2;
                eqVar = null;
                ft.a(th, "AmapLocationManager", "resultLbsLocationSuccess");
                a(aMapLocation2, eqVar);
            }
        } else {
            eqVar = null;
            aMapLocation = null;
        }
        h hVar2 = this.d;
        aMapLocation2 = hVar2 != null ? hVar2.a(aMapLocation, this.B) : aMapLocation;
        a(aMapLocation2, eqVar);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void a(Message message) {
        try {
            Bundle data = message.getData();
            AMapLocation aMapLocation = (AMapLocation) data.getParcelable("loc");
            String string = data.getString("lastLocNb");
            f(aMapLocation);
            if (this.k.a(aMapLocation, string)) {
                this.k.d();
            }
        } catch (Throwable th) {
            ft.a(th, "ALManager", "doSaveLastLocation");
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void a(AMapLocation aMapLocation) {
        try {
            if (aMapLocation.getErrorCode() != 0) {
                aMapLocation.setLocationType(0);
            }
            if (aMapLocation.getErrorCode() == 0) {
                double latitude = aMapLocation.getLatitude();
                double longitude = aMapLocation.getLongitude();
                if ((latitude == Utils.DOUBLE_EPSILON && longitude == Utils.DOUBLE_EPSILON) || latitude < -90.0d || latitude > 90.0d || longitude < -180.0d || longitude > 180.0d) {
                    fz.a("errorLatLng", aMapLocation.toStr());
                    aMapLocation.setLocationType(0);
                    aMapLocation.setErrorCode(8);
                    aMapLocation.setLocationDetail("LatLng is error#0802");
                }
            }
            if (GeocodeSearch.GPS.equalsIgnoreCase(aMapLocation.getProvider()) || !this.d.b()) {
                aMapLocation.setAltitude(gb.c(aMapLocation.getAltitude()));
                aMapLocation.setBearing(gb.a(aMapLocation.getBearing()));
                aMapLocation.setSpeed(gb.a(aMapLocation.getSpeed()));
                c(aMapLocation);
                b(aMapLocation);
                Iterator<AMapLocationListener> it = this.e.iterator();
                while (it.hasNext()) {
                    try {
                        it.next().onLocationChanged(aMapLocation);
                    } catch (Throwable unused) {
                    }
                }
            }
        } catch (Throwable unused2) {
        }
    }

    private synchronized void a(AMapLocation aMapLocation, eq eqVar) {
        if (aMapLocation == null) {
            try {
                aMapLocation = new AMapLocation("");
                aMapLocation.setErrorCode(8);
                aMapLocation.setLocationDetail("amapLocation is null#0801");
            } catch (Throwable th) {
                ft.a(th, "ALManager", "handlerLocation part3");
                return;
            }
        }
        if (!GeocodeSearch.GPS.equalsIgnoreCase(aMapLocation.getProvider())) {
            aMapLocation.setProvider("lbs");
        }
        if (this.x == null) {
            this.x = new AMapLocationQualityReport();
        }
        this.x.setLocationMode(this.b.getLocationMode());
        h hVar = this.d;
        if (hVar != null) {
            this.x.setGPSSatellites(hVar.e());
            this.x.setGpsStatus(this.d.d());
        }
        this.x.setWifiAble(gb.g(this.D));
        this.x.setNetworkType(gb.h(this.D));
        if (aMapLocation.getLocationType() == 1 || GeocodeSearch.GPS.equalsIgnoreCase(aMapLocation.getProvider())) {
            this.x.setNetUseTime(0L);
        }
        if (eqVar != null) {
            this.x.setNetUseTime(eqVar.a());
        }
        this.x.setInstallHighDangerMockApp(J);
        aMapLocation.setLocationQualityReport(this.x);
        if (this.G) {
            a(aMapLocation, this.B);
            if (eqVar != null) {
                eqVar.d(gb.b());
            }
            fz.a(this.D, aMapLocation, eqVar);
            fz.a(this.D, aMapLocation);
            d(aMapLocation.m7clone());
            fy.a(this.D).a(aMapLocation);
            fy.a(this.D).b();
        }
        if (this.q) {
            return;
        }
        if (this.b.isOnceLocation()) {
            l();
            a(14, (Bundle) null);
        }
    }

    private void a(AMapLocation aMapLocation, String str) {
        Bundle bundle = new Bundle();
        bundle.putParcelable("loc", aMapLocation);
        bundle.putString("lastLocNb", str);
        a(PointerIconCompat.TYPE_HORIZONTAL_DOUBLE_ARROW, bundle, 0L);
    }

    private static void a(er erVar) {
        try {
            erVar.d();
            erVar.a(new AMapLocationClientOption().setNeedAddress(false));
            erVar.a(true, new eq());
        } catch (Throwable th) {
            ft.a(th, "ALManager", "apsLocation:doFirstNetLocate 2");
        }
    }

    private void a(er erVar, eq eqVar) {
        try {
            erVar.a(this.D);
            erVar.a(this.b);
            erVar.b(eqVar);
        } catch (Throwable th) {
            ft.a(th, "ALManager", "initApsBase");
        }
    }

    private static void a(er erVar, ew ewVar) {
        if (ewVar != null) {
            try {
                if (ewVar.getErrorCode() == 0) {
                    erVar.b(ewVar);
                }
            } catch (Throwable th) {
                ft.a(th, "ALManager", "apsLocation:doFirstAddCache");
            }
        }
    }

    /* JADX WARN: Can't wrap try/catch for region: R(28:1|2|3|4|(1:6)|7|(1:9)|10|11|12|(3:14|(2:16|(2:87|(2:89|(1:93)))(1:18))|94)(1:95)|19|(18:(1:22)(1:85)|23|24|(2:26|(1:28))|30|(3:74|75|76)(1:32)|33|(1:37)|38|39|(1:43)|45|(1:47)|48|(1:50)|(1:55)|69|70)(1:86)|81|(0)(0)|33|(2:35|37)|38|39|(2:41|43)|45|(0)|48|(0)|(2:53|55)|69|70|(2:(0)|(1:71))) */
    /* JADX WARN: Removed duplicated region for block: B:110:0x00c2 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:60:0x00d1  */
    /* JADX WARN: Removed duplicated region for block: B:76:0x0107 A[Catch: all -> 0x0123, TryCatch #7 {all -> 0x00ce, blocks: (B:56:0x00c2, B:85:0x012d, B:87:0x0131, B:74:0x0100, B:76:0x0107, B:77:0x011a, B:79:0x011f, B:61:0x00d2, B:63:0x00da, B:65:0x00de), top: B:110:0x00c2 }] */
    /* JADX WARN: Removed duplicated region for block: B:79:0x011f A[Catch: all -> 0x0123, TRY_LEAVE, TryCatch #7 {all -> 0x00ce, blocks: (B:56:0x00c2, B:85:0x012d, B:87:0x0131, B:74:0x0100, B:76:0x0107, B:77:0x011a, B:79:0x011f, B:61:0x00d2, B:63:0x00da, B:65:0x00de), top: B:110:0x00c2 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    private com.loc.ew b(com.loc.er r14) {
        /*
            Method dump skipped, instructions count: 331
            To view this dump change 'Code comments level' option to 'DEBUG'
        */
        throw new UnsupportedOperationException("Method not decompiled: com.loc.d.b(com.loc.er):com.loc.ew");
    }

    private void b(Looper looper) {
        try {
            if (looper == null) {
                this.c = Looper.myLooper() == null ? new c(this.D.getMainLooper()) : new c();
            } else {
                this.c = new c(looper);
            }
        } catch (Throwable th) {
            ft.a(th, "ALManager", "init 1");
        }
        try {
            this.k = new j(this.D);
            b bVar = new b("amapLocManagerThread", this);
            this.p = bVar;
            bVar.setPriority(5);
            this.p.start();
            this.A = a(this.p.getLooper());
            try {
                this.d = new h(this.D, this.c);
                this.E = new g(this.D, this.c);
            } catch (Throwable th2) {
                ft.a(th2, "ALManager", "init 3");
            }
            if (this.t == null) {
                this.t = new fz();
            }
            a(this.D);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void b(Message message) {
        try {
            AMapLocation aMapLocation = (AMapLocation) message.obj;
            if (this.h && this.l != null) {
                Bundle bundle = new Bundle();
                bundle.putBundle("optBundle", ft.a(this.b));
                a(0, bundle);
                if (this.G) {
                    a(13, (Bundle) null);
                }
                this.h = false;
            }
            a(aMapLocation, (eq) null);
            a(InputDeviceCompat.SOURCE_GAMEPAD);
            a(InputDeviceCompat.SOURCE_GAMEPAD, (Object) null, 300000L);
        } catch (Throwable th) {
            ft.a(th, "ALManager", "resultGpsLocationSuccess");
        }
    }

    private static void b(AMapLocation aMapLocation) {
        if (aMapLocation == null) {
            return;
        }
        try {
            if (2 == aMapLocation.getLocationType() || 4 == aMapLocation.getLocationType()) {
                long time = aMapLocation.getTime();
                long currentTimeMillis = System.currentTimeMillis();
                if (currentTimeMillis > time) {
                    aMapLocation.setTime(currentTimeMillis);
                }
            }
        } catch (Throwable unused) {
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void c(Message message) {
        if (message == null) {
            return;
        }
        try {
            Bundle data = message.getData();
            if (data == null) {
                return;
            }
            int i = data.getInt("i", 0);
            Intent q = q();
            q.putExtra("i", i);
            q.putExtra("h", (Notification) data.getParcelable("h"));
            q.putExtra("g", 1);
            a(q, true);
        } catch (Throwable th) {
            ft.a(th, "ALManager", "doEnableBackgroundLocation");
        }
    }

    private void c(AMapLocation aMapLocation) {
        if (aMapLocation != null) {
            try {
                String locationDetail = aMapLocation.getLocationDetail();
                StringBuilder sb = TextUtils.isEmpty(locationDetail) ? new StringBuilder() : new StringBuilder(locationDetail);
                boolean c2 = gb.c(this.D, "EYW5kcm9pZC5wZXJtaXNzaW9uLkFDQ0VTU19XSUZJX1NUQVRF");
                boolean c3 = gb.c(this.D, "WYW5kcm9pZC5wZXJtaXNzaW9uLkNIQU5HRV9XSUZJX1NUQVRF");
                boolean c4 = gb.c(this.D, "WYW5kcm9pZC5wZXJtaXNzaW9uLkFDQ0VTU19MT0NBVElPTl9FWFRSQV9DT01NQU5EUw==");
                boolean c5 = gb.c(this.D, "EYW5kcm9pZC5wZXJtaXNzaW9uLlJFQURfUEhPTkVfU1RBVEU=");
                boolean c6 = gb.c(this.D, "EYW5kcm9pZC5wZXJtaXNzaW9uLkFDQ0VTU19GSU5FX0xPQ0FUSU9O");
                boolean c7 = gb.c(this.D, "EYW5kcm9pZC5wZXJtaXNzaW9uLkFDQ0VTU19DT0FSU0VfTE9DQVRJT04=");
                sb.append(c2 ? "#pm1" : "#pm0");
                String str = "1";
                sb.append(c3 ? "1" : "0");
                sb.append(c4 ? "1" : "0");
                sb.append(c5 ? "1" : "0");
                sb.append(c6 ? "1" : "0");
                if (!c7) {
                    str = "0";
                }
                sb.append(str);
                aMapLocation.setLocationDetail(sb.toString());
            } catch (Throwable unused) {
                fv.b();
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void c(AMapLocationListener aMapLocationListener) {
        if (aMapLocationListener == null) {
            throw new IllegalArgumentException("listener参数不能为null");
        }
        if (this.e == null) {
            this.e = new ArrayList<>();
        }
        if (this.e.contains(aMapLocationListener)) {
            return;
        }
        this.e.add(aMapLocationListener);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void d(Message message) {
        if (message == null) {
            return;
        }
        try {
            Bundle data = message.getData();
            if (data == null) {
                return;
            }
            boolean z = data.getBoolean("j", true);
            Intent q = q();
            q.putExtra("j", z);
            q.putExtra("g", 2);
            a(q, false);
        } catch (Throwable th) {
            ft.a(th, "ALManager", "doDisableBackgroundLocation");
        }
    }

    private void d(AMapLocation aMapLocation) {
        Message obtainMessage = this.c.obtainMessage();
        obtainMessage.what = 10;
        obtainMessage.obj = aMapLocation;
        this.c.sendMessage(obtainMessage);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void d(AMapLocationListener aMapLocationListener) {
        if (!this.e.isEmpty() && this.e.contains(aMapLocationListener)) {
            this.e.remove(aMapLocationListener);
        }
        if (this.e.isEmpty()) {
            l();
        }
    }

    private synchronized void e(AMapLocation aMapLocation) {
        if (aMapLocation == null) {
            try {
                aMapLocation = new AMapLocation("");
                aMapLocation.setErrorCode(8);
                aMapLocation.setLocationDetail("coarse amapLocation is null#2005");
            } catch (Throwable th) {
                ft.a(th, "ALManager", "handlerCoarseLocation part2");
                return;
            }
        }
        if (this.x == null) {
            this.x = new AMapLocationQualityReport();
        }
        this.x.setLocationMode(this.b.getLocationMode());
        if (this.E != null) {
            this.x.setGPSSatellites(aMapLocation.getSatellites());
            this.x.setGpsStatus(this.E.b());
        }
        this.x.setWifiAble(gb.g(this.D));
        this.x.setNetworkType(gb.h(this.D));
        this.x.setNetUseTime(0L);
        this.x.setInstallHighDangerMockApp(J);
        aMapLocation.setLocationQualityReport(this.x);
        if (this.G) {
            fz.a(this.D, aMapLocation);
            d(aMapLocation.m7clone());
            fy.a(this.D).a(aMapLocation);
            fy.a(this.D).b();
        }
        if (this.q) {
            return;
        }
        if (this.E != null) {
            l();
        }
        a(14, (Bundle) null);
    }

    private void f(AMapLocation aMapLocation) {
        if (aMapLocation == null) {
            return;
        }
        AMapLocation aMapLocation2 = null;
        try {
            if (j.b == null) {
                j jVar = this.k;
                if (jVar != null) {
                    aMapLocation2 = jVar.b();
                }
            } else {
                aMapLocation2 = j.b.a();
            }
            fz.a(aMapLocation2, aMapLocation);
        } catch (Throwable unused) {
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void g(AMapLocation aMapLocation) {
        try {
            if (this.i && this.l != null) {
                Bundle bundle = new Bundle();
                bundle.putBundle("optBundle", ft.a(this.b));
                a(0, bundle);
                if (this.G) {
                    a(13, (Bundle) null);
                }
                this.i = false;
            }
            e(aMapLocation);
        } catch (Throwable th) {
            ft.a(th, "ALManager", "resultGpsLocationSuccess");
        }
    }

    private void h() {
        synchronized (this.s) {
            a aVar = this.A;
            if (aVar != null) {
                aVar.removeCallbacksAndMessages(null);
            }
            this.A = null;
        }
    }

    private boolean i() {
        boolean z = false;
        int i = 0;
        do {
            try {
                if (this.l != null) {
                    break;
                }
                Thread.sleep(100L);
                i++;
            } catch (Throwable th) {
                ft.a(th, "ALManager", "checkAPSManager");
            }
        } while (i < 50);
        if (this.l == null) {
            Message obtain = Message.obtain();
            Bundle bundle = new Bundle();
            AMapLocation aMapLocation = new AMapLocation("");
            aMapLocation.setErrorCode(10);
            aMapLocation.setLocationDetail(!gb.k(this.D.getApplicationContext()) ? "请检查配置文件是否配置服务，并且manifest中service标签是否配置在application标签内#1003" : "启动ApsServcie失败#1001");
            bundle.putParcelable("loc", aMapLocation);
            obtain.setData(bundle);
            obtain.what = 1;
            this.c.sendMessage(obtain);
        } else {
            z = true;
        }
        if (!z) {
            fz.a((String) null, !gb.k(this.D.getApplicationContext()) ? 2103 : AMapException.CODE_AMAP_NEARBY_KEY_NOT_BIND);
        }
        return z;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public synchronized void j() {
        if ((Build.VERSION.SDK_INT < 29 && Build.VERSION.SDK_INT >= 23 && !gb.c(this.D, "EYW5kcm9pZC5wZXJtaXNzaW9uLkFDQ0VTU19DT0FSU0VfTE9DQVRJT04=") && !gb.c(this.D, "EYW5kcm9pZC5wZXJtaXNzaW9uLkFDQ0VTU19GSU5FX0xPQ0FUSU9O")) || ((Build.VERSION.SDK_INT < 31 && Build.VERSION.SDK_INT >= 29 && this.D.getApplicationInfo().targetSdkVersion >= 29 && !gb.c(this.D, "EYW5kcm9pZC5wZXJtaXNzaW9uLkFDQ0VTU19GSU5FX0xPQ0FUSU9O")) || ((Build.VERSION.SDK_INT < 31 && Build.VERSION.SDK_INT >= 29 && this.D.getApplicationInfo().targetSdkVersion < 29 && !gb.c(this.D, "EYW5kcm9pZC5wZXJtaXNzaW9uLkFDQ0VTU19DT0FSU0VfTE9DQVRJT04=") && !gb.c(this.D, "EYW5kcm9pZC5wZXJtaXNzaW9uLkFDQ0VTU19GSU5FX0xPQ0FUSU9O")) || (Build.VERSION.SDK_INT >= 31 && !gb.c(this.D, "EYW5kcm9pZC5wZXJtaXNzaW9uLkFDQ0VTU19DT0FSU0VfTE9DQVRJT04=") && !gb.c(this.D, "EYW5kcm9pZC5wZXJtaXNzaW9uLkFDQ0VTU19GSU5FX0xPQ0FUSU9O"))))) {
            k();
            return;
        }
        if (this.b == null) {
            this.b = new AMapLocationClientOption();
        }
        if (this.G) {
            return;
        }
        this.G = true;
        long j = 0;
        a(1029, (Object) null, 0L);
        int i = AnonymousClass3.a[this.b.getLocationMode().ordinal()];
        if (i == 1) {
            a(1027, (Object) null, 0L);
            a(PointerIconCompat.TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW, (Object) null, 0L);
            a(PointerIconCompat.TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW, (Object) null, 0L);
        } else if (i == 2) {
            if (gb.m(this.D)) {
                a(PointerIconCompat.TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW);
                a(PointerIconCompat.TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW, (Object) null, 0L);
                a(1026, (Object) null, 0L);
                return;
            }
            a(PointerIconCompat.TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW);
            a(1027, (Object) null, 0L);
            a(PointerIconCompat.TYPE_VERTICAL_DOUBLE_ARROW, (Object) null, 0L);
        } else {
            if (i == 3) {
                if (gb.m(this.D)) {
                    a(PointerIconCompat.TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW);
                    a(PointerIconCompat.TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW, (Object) null, 0L);
                    a(1026, (Object) null, 0L);
                    return;
                }
                a(1027, (Object) null, 0L);
                a(PointerIconCompat.TYPE_VERTICAL_DOUBLE_ARROW, (Object) null, 0L);
                if (this.b.isGpsFirst() && this.b.isOnceLocation()) {
                    j = this.b.getGpsFirstTimeout();
                }
                a(PointerIconCompat.TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW, (Object) null, j);
            }
        }
    }

    private void k() {
        AMapLocation aMapLocation = new AMapLocation("");
        aMapLocation.setErrorCode(12);
        aMapLocation.setLocationDetail("定位权限被禁用,请授予应用定位权限 #1201");
        if (this.x == null) {
            this.x = new AMapLocationQualityReport();
        }
        AMapLocationQualityReport aMapLocationQualityReport = new AMapLocationQualityReport();
        this.x = aMapLocationQualityReport;
        aMapLocationQualityReport.setGpsStatus(4);
        this.x.setGPSSatellites(0);
        this.x.setLocationMode(this.b.getLocationMode());
        this.x.setWifiAble(gb.g(this.D));
        this.x.setNetworkType(gb.h(this.D));
        this.x.setNetUseTime(0L);
        aMapLocation.setLocationQualityReport(this.x);
        fz.a((String) null, 2121);
        d(aMapLocation);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void l() {
        try {
            a(InputDeviceCompat.SOURCE_GAMEPAD);
            h hVar = this.d;
            if (hVar != null) {
                hVar.a();
            }
            g gVar = this.E;
            if (gVar != null) {
                gVar.a();
            }
            a(PointerIconCompat.TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW);
            a(1030, (Object) null, 0L);
            this.G = false;
            this.o = 0;
        } catch (Throwable th) {
            ft.a(th, "ALManager", "stopLocation");
        }
    }

    private void m() {
        ew b2 = b(new er(true));
        if (i()) {
            Bundle bundle = new Bundle();
            String str = (b2 == null || !(b2.getLocationType() == 2 || b2.getLocationType() == 4)) ? "0" : "1";
            bundle.putBundle("optBundle", ft.a(this.b));
            bundle.putString("isCacheLoc", str);
            a(0, bundle);
            if (this.G) {
                a(13, (Bundle) null);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void n() {
        try {
            if (H || !(this.u || this.O)) {
                H = false;
                this.O = true;
                m();
            } else {
                if (this.u && !a() && !this.z) {
                    this.z = true;
                    p();
                }
                if (i()) {
                    this.z = false;
                    Bundle bundle = new Bundle();
                    bundle.putBundle("optBundle", ft.a(this.b));
                    bundle.putString("d", UmidtokenInfo.getUmidtoken());
                    if (!this.d.b()) {
                        a(1, bundle);
                    }
                }
            }
        } catch (Throwable th) {
            try {
                ft.a(th, "ALManager", "doLBSLocation");
                try {
                    if (this.b.isOnceLocation()) {
                        return;
                    }
                    o();
                } catch (Throwable unused) {
                }
            } finally {
                try {
                    if (!this.b.isOnceLocation()) {
                        o();
                    }
                } catch (Throwable unused2) {
                }
            }
        }
    }

    private void o() {
        if (this.b.getLocationMode() != AMapLocationClientOption.AMapLocationMode.Device_Sensors) {
            a(PointerIconCompat.TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW, (Object) null, this.b.getInterval() >= 1000 ? this.b.getInterval() : 1000L);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void p() {
        try {
            if (this.m == null) {
                this.m = new Messenger(this.c);
            }
            a(q());
        } catch (Throwable unused) {
        }
    }

    private Intent q() {
        String str;
        if (this.n == null) {
            this.n = new Intent(this.D, APSService.class);
        }
        try {
            str = !TextUtils.isEmpty(AMapLocationClientOption.getAPIKEY()) ? AMapLocationClientOption.getAPIKEY() : l.f(this.D);
        } catch (Throwable th) {
            ft.a(th, "ALManager", "startServiceImpl p2");
            str = "";
        }
        this.n.putExtra("a", str);
        this.n.putExtra("b", l.c(this.D));
        this.n.putExtra("d", UmidtokenInfo.getUmidtoken());
        return this.n;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void r() {
        try {
            StringBuilder sb = new StringBuilder();
            new eq().f("#2001");
            sb.append("模糊权限下不支持低功耗定位#2001");
            fz.a((String) null, 2153);
            ew ewVar = new ew("");
            ewVar.setErrorCode(20);
            ewVar.setLocationDetail(sb.toString());
            g(ewVar);
        } catch (Throwable th) {
            ft.a(th, "ALManager", "apsLocation:callback");
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void s() {
        fz fzVar;
        Context context;
        int i;
        this.d.b(this.b);
        this.E.b(this.b);
        if (this.G && !this.b.getLocationMode().equals(this.r)) {
            l();
            j();
        }
        this.r = this.b.getLocationMode();
        if (this.t != null) {
            if (this.b.isOnceLocation()) {
                fzVar = this.t;
                context = this.D;
                i = 0;
            } else {
                fzVar = this.t;
                context = this.D;
                i = 1;
            }
            fzVar.a(context, i);
            this.t.a(this.D, this.b);
        }
    }

    private boolean t() {
        if (gb.j(this.D)) {
            int i = -1;
            try {
                i = fx.b(((Application) this.D.getApplicationContext()).getBaseContext(), "checkSelfPermission", "android.permission.FOREGROUND_SERVICE");
            } catch (Throwable unused) {
            }
            if (i != 0) {
                return false;
            }
        }
        return true;
    }

    public final void a(int i, Notification notification) {
        if (i == 0 || notification == null) {
            return;
        }
        try {
            if (this.j && this.l != null) {
                Bundle bundle = new Bundle();
                bundle.putBundle("optBundle", ft.a(this.b));
                a(0, bundle);
                this.j = false;
            }
            Bundle bundle2 = new Bundle();
            bundle2.putInt("i", i);
            bundle2.putParcelable("h", notification);
            a(1023, bundle2, 0L);
        } catch (Throwable th) {
            ft.a(th, "ALManager", "disableBackgroundLocation");
        }
    }

    public final void a(WebView webView) {
        if (this.M == null) {
            this.M = new i(this.D, webView);
        }
        this.M.a();
    }

    public final void a(AMapLocationClientOption aMapLocationClientOption) {
        try {
            this.L = aMapLocationClientOption.m8clone();
            a(PointerIconCompat.TYPE_ZOOM_IN, aMapLocationClientOption.m8clone(), 0L);
        } catch (Throwable th) {
            ft.a(th, "ALManager", "setLocationOption");
        }
    }

    public final void a(AMapLocationListener aMapLocationListener) {
        try {
            a(1002, aMapLocationListener, 0L);
        } catch (Throwable th) {
            ft.a(th, "ALManager", "setLocationListener");
        }
    }

    public final void a(boolean z) {
        try {
            Bundle bundle = new Bundle();
            bundle.putBoolean("j", z);
            a(1024, bundle, 0L);
        } catch (Throwable th) {
            ft.a(th, "ALManager", "disableBackgroundLocation");
        }
    }

    public final boolean a() {
        return this.F;
    }

    public final void b() {
        c cVar;
        try {
            if (this.L.getCacheCallBack() && (cVar = this.c) != null) {
                cVar.sendEmptyMessageDelayed(13, this.L.getCacheCallBackTime());
            }
        } catch (Throwable unused) {
        }
        try {
            a(1003, (Object) null, 0L);
        } catch (Throwable th) {
            ft.a(th, "ALManager", "startLocation");
        }
    }

    public final void b(AMapLocationListener aMapLocationListener) {
        try {
            a(1005, aMapLocationListener, 0L);
        } catch (Throwable th) {
            ft.a(th, "ALManager", "unRegisterLocationListener");
        }
    }

    public final void c() {
        try {
            a(1004, (Object) null, 0L);
        } catch (Throwable th) {
            ft.a(th, "ALManager", "stopLocation");
        }
    }

    public final void d() {
        try {
            i iVar = this.M;
            if (iVar != null) {
                iVar.b();
                this.M = null;
            }
            a(1011, (Object) null, 0L);
            this.q = true;
        } catch (Throwable th) {
            ft.a(th, "ALManager", "onDestroy");
        }
    }

    public final AMapLocation e() {
        AMapLocation aMapLocation = null;
        try {
            j jVar = this.k;
            if (jVar != null && (aMapLocation = jVar.b()) != null) {
                aMapLocation.setTrustedLevel(3);
            }
        } catch (Throwable th) {
            ft.a(th, "ALManager", "getLastKnownLocation");
        }
        return aMapLocation;
    }

    public final void f() {
        try {
            i iVar = this.M;
            if (iVar != null) {
                iVar.b();
                this.M = null;
            }
        } catch (Throwable th) {
            ft.a(th, "ALManager", "stopAssistantLocation");
        }
    }

    final void g() {
        a(12, (Bundle) null);
        this.h = true;
        this.i = true;
        this.j = true;
        this.F = false;
        this.u = false;
        l();
        fz fzVar = this.t;
        if (fzVar != null) {
            fzVar.b(this.D);
        }
        fy.a(this.D).a();
        fz.a(this.D);
        e eVar = this.v;
        if (eVar != null) {
            eVar.b().sendEmptyMessage(11);
        } else {
            ServiceConnection serviceConnection = this.N;
            if (serviceConnection != null) {
                this.D.unbindService(serviceConnection);
            }
        }
        try {
            if (this.C) {
                this.D.stopService(q());
            }
        } catch (Throwable unused) {
        }
        this.C = false;
        ArrayList<AMapLocationListener> arrayList = this.e;
        if (arrayList != null) {
            arrayList.clear();
            this.e = null;
        }
        this.N = null;
        h();
        if (this.p != null) {
            if (Build.VERSION.SDK_INT >= 18) {
                try {
                    fx.a(this.p, HandlerThread.class, "quitSafely", new Object[0]);
                } catch (Throwable unused2) {
                }
            }
            this.p.quit();
        }
        this.p = null;
        c cVar = this.c;
        if (cVar != null) {
            cVar.removeCallbacksAndMessages(null);
        }
        j jVar = this.k;
        if (jVar != null) {
            jVar.c();
            this.k = null;
        }
    }
}
